package org.swqsbssddqs.shortlink.admin.remote;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.swqsbssddqs.shortlink.admin.common.convention.result.Result;
import org.swqsbssddqs.shortlink.admin.dto.req.*;
import org.swqsbssddqs.shortlink.admin.dto.resp.ShortLinkStatsRespDTO;
import org.swqsbssddqs.shortlink.admin.remote.dto.req.*;
import org.swqsbssddqs.shortlink.admin.remote.dto.resp.*;

import java.util.HashMap;
import java.util.List;

public interface ShortLinkRemoteService {

    default Result<IPage<ShortLinkPageRespDTO>> pageShortLink(ShortLinkPageReqDTO shortLinkPageReqDTO) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("gid",shortLinkPageReqDTO.getGid());
        map.put("current",shortLinkPageReqDTO.getCurrent());
        map.put("size",shortLinkPageReqDTO.getSize());
        map.put("orderTag",shortLinkPageReqDTO.getOrderTag());
        String resultPageStr = HttpUtil.get("http://127.0.0.1:8001/api/short-link/v1/page", map);
        return JSON.parseObject(resultPageStr, new TypeReference<>() {});
    }

    default Result<ShortLinkCreateRespDTO> createShortLink(ShortLinkCreatReqDTO shortLinkCreatReqDTOS) {
        String resultPageStr = HttpUtil.post("http://127.0.0.1:8001/api/short-link/v1/create", JSON.toJSONString(shortLinkCreatReqDTOS));
        return JSON.parseObject(resultPageStr, new TypeReference<>() {});
    }


    default Result<List<ShortLinkGroupCountQueryRespDTO>> listGroupShortLinkCount(List<String> gidList) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("requestParam",gidList);
        String resultPageStr = HttpUtil.get("http://127.0.0.1:8001/api/short-link/v1/count", map);
        return JSON.parseObject(resultPageStr,new TypeReference<>() {});
    }

    default Result<Void> updateShortLink(ShortLinkUpdateReqDTO shortLinkUpdateReqDTO) {
        String resultPageStr = HttpUtil.post("http://127.0.0.1:8001/api/short-link/v1/update", JSON.toJSONString(shortLinkUpdateReqDTO));
        return JSON.parseObject(resultPageStr, new TypeReference<>() {});
    }

    default Result<String> getTitleByUrl(String url){

        String resultPageStr = HttpUtil.get("http://127.0.0.1:8001/api/short-link/v1/title?url="+url);
        return JSON.parseObject(resultPageStr, new TypeReference<>() {});
    };

    default void saveRecycleBin(RecycleBinSaveReqDTO requestParam){
        String resultPageStr = HttpUtil.post("http://127.0.0.1:8001/api/short-link/v1/recycle-bin/save", JSON.toJSONString(requestParam));
    };

    default Result<IPage<ShortLinkPageRespDTO>> pageRecycleBinShortLink(ShortLinkRecycleBinPageReqDTO requestParam){
        HashMap<String, Object> map = new HashMap<>();
        map.put("current",requestParam.getCurrent());
        map.put("size",requestParam.getSize());
        map.put("gidList",requestParam.getGidList());
        String resultPageStr = HttpUtil.get("http://127.0.0.1:8001/api/short-link/v1/recycle-bin/page", map);
        return JSON.parseObject(resultPageStr, new TypeReference<>() {});
    };

    default void recoverRecycleBin(RecycleBinRecoverReqDTO requestParam){
        String resultPageStr = HttpUtil.post("http://127.0.0.1:8001/api/short-link/v1/recycle-bin/recover", JSON.toJSONString(requestParam));
    };


    default void removeRecycleBin(RecycleBinRemoveReqDTO requestParam){
        String resultPageStr = HttpUtil.post("http://127.0.0.1:8001/api/short-link/v1/recycle-bin/remove", JSON.toJSONString(requestParam));
    };

    default Result<ShortLinkStatsRespDTO> oneShortLinkStats(ShortLinkStatsReqDTO requestParam){
        String resultPageStr = HttpUtil.get("http://127.0.0.1:8001/api/short-link/v1/stats", BeanUtil.beanToMap(requestParam));
        return JSON.parseObject(resultPageStr, new TypeReference<>() {});
    };

    default Result<Page<ShortLinkStatsAccessRecordRespDTO>> shortLinkStatsAccessRecord(ShortLinkStatsAccessRecordReqDTO requestParam){
        HashMap<String, Object> map = new HashMap<>();
        map.put("current",requestParam.getCurrent());
        map.put("size",requestParam.getSize());
        map.put("enableStatus",requestParam.getEnableStatus());
        map.put("startDate",requestParam.getStartDate());
        map.put("endDate",requestParam.getEndDate());
        map.put("gid",requestParam.getGid());
        map.put("fullShortUrl",requestParam.getFullShortUrl());

        String resultPageStr = HttpUtil.get("http://127.0.0.1:8001/api/short-link/v1/stats/access-record",map);
        return JSON.parseObject(resultPageStr, new TypeReference<>() {});
    };

    default Result<Page<ShortLinkStatsGroupAccessRecordRespDTO>> shortLinkGroupStatsAccessRecord(ShortLinkGroupStatsAccessRecordReqDTO requestParam) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("current",requestParam.getCurrent());
        map.put("size",requestParam.getSize());
        map.put("enableStatus",requestParam.getEnableStatus());
        map.put("startDate",requestParam.getStartDate());
        map.put("endDate",requestParam.getEndDate());
        map.put("gid",requestParam.getGid());

        String resultPageStr = HttpUtil.get("http://127.0.0.1:8001/api/short-link/v1/stats/access-record/group",map);
        return JSON.parseObject(resultPageStr, new TypeReference<>() {});
    }

    default Result<ShortLinkBatchCreateRespDTO> batchCreateShortLink(ShortLinkBatchCreateReqDTO requestParam){
        String resultPageStr = HttpUtil.post("http://127.0.0.1:8001/api/short-link/v1/create/batch", JSON.toJSONString(requestParam));
        return JSON.parseObject(resultPageStr, new TypeReference<>() {});
    };
}
